package com.eascs.web.o2o.web.aging.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.report.common.entity.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.o2o.web.aging.dubbo.AgingDubboServiceImpl;
import com.eascs.web.o2o.web.aging.entity.ProcessTaskMain;
import com.eascs.web.o2o.web.aging.form.NodeAgingForm;
import com.eascs.web.o2o.web.aging.form.ProcessAgingForm;
import com.eascs.web.o2o.web.aging.service.IAgingService;
import com.eascs.web.o2o.web.aging.service.TemplateService;
import com.eascs.web.o2o.web.aging.service.WFQueryService;
import com.eascs.web.o2o.web.aging.utils.IntToDate;
import com.eascs.workflow.common.entity.wf.DicData;

@RestController
@RequestMapping("/raging")
public class AgingInfoListRestController extends BaseRestController {

	private static Logger logger = LoggerFactory.getLogger(AgingInfoListRestController.class);
	
	@Autowired
	IAgingService agingService;

	/*@Autowired
	IAgingDevService agingDevService;*/

	@Autowired
	TemplateService templateService;
	
	@Autowired
	WFQueryService wfQueryService;


	@RequestMapping("/processDetail")
	public PaginationRestResult detail(String billCode, String processType) {
		PageResult<Object[]> agingDetail = agingService.getAgingDetail(billCode, processType);
		String[] titles = { "流程节点", "开始时间", "结束时间", "节点用时", "节点时效", "节点超时" };
		agingDetail = IntToDate.toDate(agingDetail, 3, 5);
		List<Object[]> data = agingDetail.getData();

		for (int i = 0, len = data.size(); i < len; i++) {
			if ("开始节点".equals(data.get(i)[0]) || "结束节点".equals(data.get(i)[0])) {
				for (int j = 1, len2 = data.get(i).length; j < len2; j++) {
					data.get(i)[j] = null;
				}
			}
		}

		PaginationInfo paginationInfo = new PaginationInfo(agingDetail, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(0);
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@Autowired
	AgingDubboServiceImpl agingDubboServiceImpl;

	/**
	 * 
	 * @Title: index
	 * @Description: 节点时效（已结束流程）
	 * @author user
	 * @param entity
	 * @return
	 * @return String
	 * @throws @date
	 */
	@RequestMapping("/node_aging_list")
	public PaginationRestResult list(NodeAgingForm entity) {
		String agingUrl = "/raging/node_aging_list";
		entity.setPageSize(50);
		String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
		String catagory = entity.getCatagory() == null ? "" : entity.getCatagory();
		String processType = entity.getProcessType() == null ? "" : entity.getProcessType();
		String nodesNa = entity.getNodesNa() == null ? "" : entity.getNodesNa();
		String nodeAuditors = entity.getNodeAuditors() == null ? "" : entity.getNodeAuditors();
		String NodeTimeS = entity.getNodeTimeS() == null ? "" : entity.getNodeTimeS();
		String NodeTimeE = entity.getNodeTimeE() == null ? "" : entity.getNodeTimeE();
		String dateStart = entity.getDateStart() == null ? "" : entity.getDateStart();
		String dateEnd = entity.getDateEnd() == null ? "" : entity.getDateEnd();
		String nodeStatus = entity.getNodeStatus() == null ? "" : entity.getNodeStatus();
		String dateStartb = entity.getDateStartb() == null ? "" : entity.getDateStartb();
		String dateEndb = entity.getDateEndb() == null ? "" : entity.getDateEndb();

		agingUrl = UrlUtil.appendUrl(agingUrl, "billCode", billCode);
		agingUrl = UrlUtil.appendUrl(agingUrl, "catagory", catagory);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processType", processType);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodesNa", nodesNa);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodeAuditors", nodeAuditors);
		agingUrl = UrlUtil.appendUrl(agingUrl, "NodeTimeS", NodeTimeS);
		agingUrl = UrlUtil.appendUrl(agingUrl, "NodeTimeE", NodeTimeE);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStart", dateStart);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEnd", dateEnd);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStartb", dateStartb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEndb", dateEndb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodeStatus", nodeStatus);
		agingUrl = agingUrl + "&pageIndex=";
		// 构建流程类型数据
		if (StringUtils.isNotBlank(entity.getCatagory())) {
			List<DicData> dicList = templateService.getProcessTypeByCatagory(entity.getCatagory()).getModel();
			ViewContext.put("processTypeList", dicList);
			List<String> processTypeList = new ArrayList<String>();
			if (StringUtils.isNotBlank(entity.getProcessType())) {
				processTypeList.add(entity.getProcessType());
			} else {
				for (DicData dic : dicList) {
					processTypeList.add(dic.getCode());
				}
			}
			entity.setProcessTypeList(processTypeList);
		}
		PageResult<Object[]> agingInfoByPage = agingService.listNodeAgingInfoFinished(entity);
		String titles[] = { "单号", "流程类型", "节点名称", "节点处理人", "节点开始时间", "节点结束时间", "节点用时/（小时）" };
		PaginationInfo paginationInfo = new PaginationInfo(agingInfoByPage, titles, agingUrl);
		Button showButton = ButtonHelper.createAjaxButton("查看详情", "/raging/agreeInfo?taskId=");
		Button[] titleButton = new Button[] { showButton };
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(titleButton);
		paginationInfo.setRendTo("#node_aging_result");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	/**
	 * 
	 * @Title: index
	 * @Description: 节点时效（未结束流程）
	 * @author user
	 * @param entity
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年9月21日 下午2:26:33
	 */
	@RequestMapping("/node_aging_list2")
	public PaginationRestResult list2(NodeAgingForm entity) {
		String agingUrl = "/raging/node_aging_list2";
		entity.setPageSize(50);
	
		String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
		String catagory = entity.getCatagory() == null ? "" : entity.getCatagory();
		String processType = entity.getProcessType() == null ? "" : entity.getProcessType();
		String nodesNa = entity.getNodesNa() == null ? "" : entity.getNodesNa();
		String nodeAuditors = entity.getNodeAuditors() == null ? "" : entity.getNodeAuditors();
		String NodeTimeS = entity.getNodeTimeS() == null ? "" : entity.getNodeTimeS();
		String NodeTimeE = entity.getNodeTimeE() == null ? "" : entity.getNodeTimeE();
		String dateStart = entity.getDateStart() == null ? "" : entity.getDateStart();
		String dateEnd = entity.getDateEnd() == null ? "" : entity.getDateEnd();
		String nodeStatus = entity.getNodeStatus() == null ? "" : entity.getNodeStatus();
		String dateStartb = entity.getDateStartb() == null ? "" : entity.getDateStartb();
		String dateEndb = entity.getDateEndb() == null ? "" : entity.getDateEndb();

		agingUrl = UrlUtil.appendUrl(agingUrl, "billCode", billCode);
		agingUrl = UrlUtil.appendUrl(agingUrl, "catagory", catagory);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processType", processType);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodesNa", nodesNa);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodeAuditors", nodeAuditors);
		agingUrl = UrlUtil.appendUrl(agingUrl, "NodeTimeS", NodeTimeS);
		agingUrl = UrlUtil.appendUrl(agingUrl, "NodeTimeE", NodeTimeE);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStart", dateStart);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEnd", dateEnd);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStartb", dateStartb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEndb", dateEndb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "nodeStatus", nodeStatus);
		agingUrl = agingUrl + "&pageIndex=";
		// 构建流程类型数据
		if (StringUtils.isNotBlank(entity.getCatagory())) {
			List<DicData> dicList = templateService.getProcessTypeByCatagory(entity.getCatagory()).getModel();
			ViewContext.put("processTypeList", dicList);
			List<String> processTypeList = new ArrayList<String>();
			if (StringUtils.isNotBlank(entity.getProcessType())) {
				processTypeList.add(entity.getProcessType());
			} else {
				for (DicData dic : dicList) {
					processTypeList.add(dic.getCode());
				}
			}
			entity.setProcessTypeList(processTypeList);
		}
	
		PageResult<Object[]> agingInfoByPage = agingService.listNodeAgingInfoUnfinished(entity);
		String titles[] = { "单号", "流程类型", "节点名称", "节点处理人", "节点开始时间", "节点结束时间", "节点用时/（小时）"};
		PaginationInfo paginationInfo = new PaginationInfo(agingInfoByPage, titles, agingUrl);
		Button showButton = ButtonHelper.createAjaxButton("查看详情", "/raging/agreeInfo?taskId=");
		Button[] titleButton = new Button[] { showButton };
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(titleButton);
		paginationInfo.setRendTo("#node_aging_result2");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	
	
	/**
	 * 根据流程种类获取流程类型
	 * 
	 * @param catagory
	 * @return
	 */
	@RequestMapping(value = "/wf/getProccessType", method = RequestMethod.GET)
	public BaseRestResult<List<DicData>> getProcessTypeByCatagory(@RequestParam("catagory") String catagory) {
		List<DicData> data = new ArrayList<DicData>();
		if (StringUtil.hasText(catagory)) {
			ModelResult<List<DicData>> modelResult = templateService.getProcessTypeByCatagory(catagory);
			data = modelResult.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}

	@RequestMapping("/agreeInfo")
	public HtmlRestResult taskDetail(String taskId) {
		ViewContext.put("agreeUrl", "/raging/processNode/nodeList?taskId=" + taskId);
		return HtmlRestResult.popHtmlFragment("aging/nodeAgingDetail");
	}

	/**
	 * 
	 * @Title: index
	 * @Description:流程时效（已结束流程）
	 * @author user
	 * @param entity
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年9月21日 下午2:26:33
	 */
	@RequestMapping("/process_aging_list")
	public PaginationRestResult process_aging_list_finished(ProcessAgingForm entity) {
		String agingUrl = "/raging/process_aging_list";
		entity.setPageSize(50);
		String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
		String catagory = entity.getCatagory() == null ? "" : entity.getCatagory();
		String processType = entity.getProcessType() == null ? "" : entity.getProcessType();
		String processTimeS = entity.getProcessTimeS() == null ? "" : entity.getProcessTimeS();
		String processTimeE = entity.getProcessTimeE() == null ? "" : entity.getProcessTimeE();
		String dateStart = entity.getDateStart() == null ? "" : entity.getDateStart();
		String dateEnd = entity.getDateEnd() == null ? "" : entity.getDateEnd();
		String dateStartb = entity.getDateStartb() == null ? "" : entity.getDateStartb();
		String dateEndb = entity.getDateEndb() == null ? "" : entity.getDateEndb();
		String processStatus = entity.getProcessStatus() == null ? "" : entity.getProcessStatus();
		String province = entity.getProvince() == null ? "" : entity.getProvince();
		agingUrl = UrlUtil.appendUrl(agingUrl, "billCode", billCode);
		agingUrl = UrlUtil.appendUrl(agingUrl, "catagory", catagory);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processType", processType);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processTimeS", processTimeS);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processTimeE", processTimeE);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStart", dateStart);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEnd", dateEnd);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processStatus", processStatus);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStartb", dateStartb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEndb", dateEndb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "province", province);
		agingUrl = agingUrl + "&pageIndex=";
		// 构建流程类型数据
		if (StringUtils.isNotBlank(entity.getCatagory())) {
			List<DicData> dicList = templateService.getProcessTypeByCatagory(entity.getCatagory()).getModel();
			ViewContext.put("processTypeList", dicList);
			List<String> processTypeList = new ArrayList<String>();
			if (StringUtils.isNotBlank(entity.getProcessType())) {
				processTypeList.add(entity.getProcessType());
			} else {
				for (DicData dic : dicList) {
					processTypeList.add(dic.getCode());
				}
			}
			entity.setProcessTypeList(processTypeList);
		}
		PageResult<Object[]> agingInfoByPage = agingService.listProcessAgingInfoFinished(entity);
		String titles[] = { "单号", "省区", "流程类型", "流程开始时间", "流程结束时间", "流程用时/（小时）" };
		PaginationInfo paginationInfo = new PaginationInfo(agingInfoByPage, titles, agingUrl);
		Button showButton = ButtonHelper.createAjaxButton("查看详情", "/raging/agreeInfo?taskId=");
		Button[] titleButton = new Button[] { showButton };
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(titleButton);
		paginationInfo.setRendTo("#process_aging_result");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	/**
	 * 
	 * @Title: index
	 * @Description:流程时效（未结束流程）
	 * @author user
	 * @param entity
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年9月21日 下午2:26:33
	 */
	@RequestMapping("/process_aging_list2")
	public PaginationRestResult process_aging_list_unfinished(ProcessAgingForm entity) {
		String agingUrl = "/raging/process_aging_list2";
		entity.setPageSize(50);
		String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
		String catagory = entity.getCatagory() == null ? "" : entity.getCatagory();
		String processType = entity.getProcessType() == null ? "" : entity.getProcessType();
		String processTimeS = entity.getProcessTimeS() == null ? "" : entity.getProcessTimeS();
		String processTimeE = entity.getProcessTimeE() == null ? "" : entity.getProcessTimeE();
		String dateStart = entity.getDateStart() == null ? "" : entity.getDateStart();
		String dateEnd = entity.getDateEnd() == null ? "" : entity.getDateEnd();
		String dateStartb = entity.getDateStartb() == null ? "" : entity.getDateStartb();
		String dateEndb = entity.getDateEndb() == null ? "" : entity.getDateEndb();
		String processStatus = entity.getProcessStatus() == null ? "" : entity.getProcessStatus();
		agingUrl = UrlUtil.appendUrl(agingUrl, "billCode", billCode);
		agingUrl = UrlUtil.appendUrl(agingUrl, "catagory", catagory);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processType", processType);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processTimeS", processTimeS);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processTimeE", processTimeE);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStart", dateStart);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEnd", dateEnd);
		agingUrl = UrlUtil.appendUrl(agingUrl, "processStatus", processStatus);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateStartb", dateStartb);
		agingUrl = UrlUtil.appendUrl(agingUrl, "dateEndb", dateEndb);
		agingUrl = agingUrl + "&pageIndex=";
		// 构建流程类型数据
		if (StringUtils.isNotBlank(entity.getCatagory())) {
			List<DicData> dicList = templateService.getProcessTypeByCatagory(entity.getCatagory()).getModel();
			ViewContext.put("processTypeList", dicList);
			List<String> processTypeList = new ArrayList<String>();
			if (StringUtils.isNotBlank(entity.getProcessType())) {
				processTypeList.add(entity.getProcessType());
			} else {
				for (DicData dic : dicList) {
					processTypeList.add(dic.getCode());
				}
			}
			entity.setProcessTypeList(processTypeList);
		}
		PageResult<Object[]> agingInfoByPage = agingService.listProcessAgingInfoUnfinished(entity);
		String titles[] = { "单号", "省区", "流程类型", "流程开始时间", "流程结束时间", "流程用时/（小时）" };
		PaginationInfo paginationInfo = new PaginationInfo(agingInfoByPage, titles, agingUrl);
		Button showButton = ButtonHelper.createAjaxButton("查看详情", "/raging/agreeInfo?taskId=");
		Button[] titleButton = new Button[] { showButton };
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(titleButton);
		paginationInfo.setRendTo("#process_aging_result2");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
	@RequestMapping("/processNode/nodeList")
	public HtmlRestResult agreeInfo(String taskId) {
		try {
			ModelResult<ProcessTaskMain> result = wfQueryService.queryAgreeDetailWithTaskId(taskId);
			if (result.isSuccess()) {
				ProcessTaskMain taskMain = result.getModel();
				ViewContext.put("taskMain", taskMain);
			} else {
				logger.error("获取贷款单审批意见失败，errorMsg:{}", result.getErrMsg());
			}
		} catch (Exception e) {
			logger.error("query error", e);
		}
		return HtmlRestResult.html("/wf/processNode/agreeInfo");
	}
}
